﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
using Model.Enum;

namespace Controls
{
    public class TextBoxX : TextBox
    {
        private bool _isKeepFonValue;

        /// <summary>
        /// 保存后是否清空上次的值，简而言之，是否保存原值
        /// </summary>
        public bool IsKeepFonValue
        {
            get { return _isKeepFonValue; }
            set { _isKeepFonValue = value; }
        }

        private bool _isDescBox;

        /// <summary>
        /// 是否是代码的描述框
        /// </summary>
        public bool IsDescBox
        {
            get { return _isDescBox; }
            set { _isDescBox = value; }
        }

        /// <summary>
        /// 原始值
        /// </summary>
        private string _oldValue;

        private bool _isEditing = false;

        /// <summary>
        /// 当前控件是否正处于编辑状态
        /// </summary>
        public bool IsEditing
        {
            get { return _isEditing; }
            set { _isEditing = value; }
        }

        private string _editMode;

        public virtual string EditMode
        {
            get { return _editMode; }
            set { _editMode = value; }
        }

        private bool _isRequie = false;

        public virtual bool IsRequie
        {
            get { return _isRequie; }
            set { _isRequie = value; }
        }

        private PsDataType _dbType = PsDataType.String;

        public virtual PsDataType DbType
        {
            get { return _dbType; }
            set { _dbType = value; }
        }

        private string _lable;

        public virtual string Lable
        {
            get { return _lable; }
            set { _lable = value; }
        }

        private bool _sysReadOnly = false;

        public virtual bool SysReadOnly
        {
            get { return _sysReadOnly; }
            set { _sysReadOnly = value; }
        }

        private string _displayMode;

        public string DisplayMode
        {
            get { return _displayMode; }
            set { _displayMode = value; }
        }

        protected override void WndProc(ref Message m)
        {
            //设置左右对齐
            //SetAlign();
            base.WndProc(ref m);

            if ((m.Msg == 0xf || m.Msg == 0x14 || m.Msg == 0x85))
            {
                if (this.TextAlign == HorizontalAlignment.Right) return;

                using (Graphics g = Graphics.FromHwnd(this.Handle))
                {
                    //this.BorderStyle = BorderStyle.None;

                    using (Pen pen = new Pen(Color.Red))
                    {
                        if (RanderOther(g, pen))
                        {
                            if (this.IsRequie && this.Enabled && !this.ReadOnly)
                            {
                                //g.DrawRectangle(pen, 0, 0, this.Width - 1, this.Height - 1);
                                g.DrawImage(Resource1.notnull, this.Width - 15, 3);
                            }
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 数字右对齐
        /// </summary>
        private void SetAlign()
        {
            if (this.DbType == PsDataType.Num || this.DbType == PsDataType.NumThanZero || this.DbType == PsDataType.NumThanZeroOrEqualZero
                || this.DbType == PsDataType.Int || this.DbType == PsDataType.IntThanZero || this.DbType == PsDataType.IntThanZeroOrEqualZero)
            {
                this.TextAlign = HorizontalAlignment.Right;
            }
        }

        /// <summary>
        /// 绘制其他的元素 返回值表示，是否继续绘制
        /// </summary>
        /// <param name="g"></param>
        /// <param name="pen"></param>
        protected virtual bool RanderOther(Graphics g, Pen pen)
        {
            return true;
        }

        private string _defaultValue;

        public string DefaultValue
        {
            get { return _defaultValue; }
            set { _defaultValue = value; }
        }

        public delegate void DgValueChanged(object sender, EventArgs e);

        /// <summary>
        /// 值改变时候触发
        /// </summary>
        public event DgValueChanged ValueChanged;

        protected virtual void OnValueChanged(object source, EventArgs args)
        {
            if (this.ValueChanged != null) { this.ValueChanged(source, args); } //调用事件
        }

        protected override void OnEnter(EventArgs e)
        {
            if (this.ReadOnly == false)
            {
                _isEditing = true;
            }

            base.OnEnter(e);
            //记录原始值
            _oldValue = this.Text;
        }

        protected override void OnLeave(EventArgs e)
        {
            base.OnLeave(e);

            if (_oldValue != this.Text) //值已经改变,仅仅是用户输入的情况，如果用户是通过程序自动填写值的方式，就监控不到了
            {
                OnValueChanged(this, e);
                _autoMark = false;
            }

            _isEditing = false;
        }

        protected override void OnTextChanged(EventArgs e)
        {
            base.OnTextChanged(e);

            if (!this.IsEditing && !_autoMark) //当前控件不是编辑状态，可能是直接XXX.Text设置值的
            {
                OnValueChanged(this, e);
                _autoMark = false;
            }
        }

        /// <summary>
        /// 恢复(不会触发ValueChange)
        /// </summary>
        public void Revocation()
        {
            _autoMark = true;

            this.Text = _oldValue;
            if (this.Visible && this.Enabled)
            {
                this.Focus(); //让自己得到焦点
            }
        }

        bool _autoMark = false;


        private string _formatString;
        public string FormatString
        {
            get { return _formatString; }
            set { _formatString = value; }
        }

    }
}
